<?php
class ChitieunvsController extends AppController {

	var $name = 'Chitieunvs';	
	var $helpers = array('Js','Html','Form','Ctable');
	var $uses = array('Chitieunv','Diemchuan','Chitieudv','Kehoach','Kqbanhang');
	var $components = array('RequestHandler');
	
	function index() {
		$this->Chitieunv->recursive = 0;
		$this->set('chitieunvs', $this->paginate());
	}

	/*
	 *  chi tieu thang cua Trung Tam
	 */
	function themchitieu($kehoach_id=null,$themchitieu=1)
	{
		if ($kehoach_id==null)
		{
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		}
		else 
		{
		/*
		 * kiem tra da lap ke hoach chua
		 */
			$dalapkh = $this->Chitieunv->Kehoach->find('list',array('conditions'=>array('Kehoach.id'=>$kehoach_id)));
			if(count($dalapkh)==0)
			{
				$this->Session->setFlash(__('Chỉ Tiêu Chưa Thể Lập Do Chưa Lập Thời Gian Cho Kế Hoạch', true));
				$this->redirect(array('controller'=>'kehoaches','action'=>'index',$this->Session->read('cur_dv')));
			}
			$kehoaches = $this->Chitieunv->Kehoach->find('list',array('conditions'=>array('id'=>$kehoach_id),
													'fields'=>array('dagiao')));
			$dagiao = $kehoaches[$kehoach_id];
			if ($dagiao==1)
			{
				$this->Session->setFlash(__('Chỉ Tiêu Đã Giao Không Được Sửa', true));
				$this->redirect(array('action'=>'hienchitieu',$kehoach_id));
			}
		if($themchitieu==1)
		{
			/*
			 * Kiem tra kehoach nay da giao chi tieu chua
			 */
			$dalap=$this->Chitieunv->find('list',array(
												'conditions'=>array('Chitieunv.kehoach_id'=>$kehoach_id)));
			if (count($dalap)!=0)
			{
				$this->Session->setFlash(__('Chỉ Tiêu Tháng Này Đã Được Lập', true));
				$this->redirect(array('controller'=>'chitieunvs','action'=>'hienchitieuthang',$kehoach_id));
			}
		}
		$donvi_id = $this->Session->read('cur_dv');
		
		/// lay hang hoa
		$counthh = $this->data['Chitieunv']['countcheckhh'];
		$countnv = $this->data['Chitieunv']['countchecknv'];
		
		//debug($this->data['Chitieunv']);
		for ($i=0;$i<$counthh;$i++)
		{
			if ($this->data['Chitieunv']['checkboxhh'.$i]!='0')
				$hanghoas[$this->data['Chitieunv']['checkboxhh'.$i]]=$this->data['Chitieunv']['hh'][$this->data['Chitieunv']['checkboxhh'.$i]];
		} 
		
		///nhan vien col2
		for ($i=0;$i<$countnv;$i++)
		{
			if ($this->data['Chitieunv']['checkboxnv'.$i]!=0)
				$nhanviens[$this->data['Chitieunv']['checkboxnv'.$i]]=$this->data['Chitieunv'][$this->data['Chitieunv']['checkboxnv'.$i]];
		}
		//$nv_id = array_keys($nvs);
		//$nhanviens = $this->
		// diem chuan 
		$col1[]="Điểm Chuẩn";
		//lay thong tin ke hoach tu chi nhanh
		$data = $this->laykhtt_tu_cn($kehoach_id,$donvi_id);
		////
		if (!empty($data))
		{
			$col_ctdv =$data['col_ctdv'];
			$row_ctdv = $data['row_ctdv'];
			$datadv= $data['datadv'];
			$datadc=$data['datadc'];
			$col_dc  = $data['col1'];
		}
		$this->set(compact('kehoach_id','col_dc','col_ctdv','row_ctdv','datadv','datadc','col1','nhanviens','hanghoas'));
		}
	}
	
	function suachitieuthang($kehoach_id=null)
	{
		
		if ($kehoach_id==null)
		{
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		}
		else 
		{
		/*
		 * Kiem tra kehoach nay da giao chi tieu chua
		 */
		$dalap=$this->Chitieunv->find('list',array(
											'conditions'=>array('Chitieunv.kehoach_id'=>$kehoach_id)));
		if (count($dalap)==0)
		{
			$this->Session->setFlash(__('Chỉ tiêu chưa giao nên không thể sửa', true));
			$this->redirect(array('controller'=>'chitieunvs','action'=>'hienchitieuthang',$kehoach_id));
		}
		$kehoaches = $this->Chitieunv->Kehoach->find('list',array('conditions'=>array('id'=>$kehoach_id),
													'fields'=>array('dagiao')));
		$dagiao = $kehoaches[$kehoach_id];
		if ($dagiao==1)
		{
			$this->Session->setFlash(__('Chỉ tiêu đã giao không được sửa', true));
			$this->redirect(array('action'=>'hienchitieu',$kehoach_id));
		}
		/*
		 * lay data cho diem chuan
		 */
		$diems =$this->Diemchuan->find('list',array(
								'conditions'=>array('kehoach_id'=>$kehoach_id),
								'fields'=>array('hanghoa_id','diem')));
		$diemchuanid =$this->Diemchuan->find('list',array(
								'conditions'=>array('kehoach_id'=>$kehoach_id),
								'fields'=>array('hanghoa_id','id')));
		$col1[]="Điểm Chuẩn";
		/*
		 * lay data cua chitieu
		 */
		$thoigian = $this->Chitieunv->Kehoach->find('list',array(
																	'recursive'=>0,'contain'=>array('Thoigian'),
																	'conditions'=>array('Kehoach.id'=>$kehoach_id),
																	'fields'=>array('Thoigian.ngaybd','Thoigian.ngaykt')));
		$hanghoa = $this->Chitieunv->find('list',array(
										'recursive'=>0,'contain'=>array('Hanghoa'),
										'conditions'=>array('Chitieunv.kehoach_id'=>$kehoach_id),
										'fields'=>array('Chitieunv.hanghoa_id','Hanghoa.tenhh')));
		$nhanviens = $this->Chitieunv->find('list',array(
										'recursive'=>0,'contain'=>array('Nhanvien'),
										'conditions'=>array('Chitieunv.kehoach_id'=>$kehoach_id),
										'fields'=>array('Chitieunv.nhanvien_id','Nhanvien.tennv')));
		$chitieunvs = $this->Chitieunv->find('list',array(
											'conditions'=>array('Chitieunv.kehoach_id'=>$kehoach_id),
											'fields'=>array('Chitieunv.hanghoa_id','Chitieunv.soluong','Chitieunv.nhanvien_id')));
		
		$chitieunvs_id = $this->Chitieunv->find('list',array(
											'conditions'=>array('kehoach_id'=>$kehoach_id),
											'fields'=>array('Chitieunv.hanghoa_id','Chitieunv.id','Chitieunv.nhanvien_id')));
		if(!empty($chitieunvs)){
		$dong = 0;
		$cot = 0;
		foreach ($nhanviens as $colkey => $colvalue):
			$dong=0;
			foreach ($hanghoa as $rowkey=>$rowvalue):
				$data2[$dong][$cot]= $chitieunvs[$colkey][$rowkey];
				$data_id2[$dong][$cot]=$chitieunvs_id[$colkey][$rowkey];
				$data1[$dong][0]=$diems[$rowkey];
				$data_id1[$dong][0]= $diemchuanid[$rowkey];
				$dong++;
			endforeach;
			$cot++;
		endforeach;
		}
		$this->set(compact('col1','nhanviens','hanghoa','data1','data_id1','data2','data_id2','kehoach_id','thoigian'));
		}
	}
	
	function luuchitieuthang($kehoach_id=null)
	{
		
		if ($kehoach_id==null)
		{
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		}
		else 
		{
		/*
		 * lay data
		 */
		$colnum2= $this->data['Chitieunv']['colnum2'];
		$rownum = $this->data['Chitieunv']['rownum'];
		$thoigian = $this->Chitieunv->Kehoach->find('list',array(
																'recursive'=>0,'contain'=>array('Thoigian'),
																'conditions'=>array('Kehoach.id'=>$kehoach_id),
																'fields'=>array('Thoigian.ngaybd','Thoigian.ngaykt','Thoigian.id')));
		$thoigian_id=key($thoigian);
		
		/*
		 *  Luu Diem chuan
		 */
		 for($j=0;$j<$rownum;$j++)
	     {
			if(isset($this->data['Chitieunv']['col_1']['id'][$j][0]))
				$id_dc=$this->data['Chitieunv']['col_1']['id'][$j][0]; 
	        $kehoach_dc=$kehoach_id;
	        	 $hanghoa_dc = $this->data['Chitieunv']['row'][$j];
            
            
	        
            if ($this->data['Chitieunv']['col_1'][$j][0]!=NULL)
	        	$diemchuan=$this->data['Chitieunv']['col_1'][$j][0];
	        else
	            $diemchuan=0;
	        if(isset($id_dc))
	         	$qstr= "update diemchuans set diem=".$diemchuan." where id=".$id_dc;
	        else
	           	$qstr= "insert into diemchuans(hanghoa_id,kehoach_id,diem) values(".$hanghoa_dc.",".$kehoach_dc.",".$diemchuan.")";
	        $this->Chitieunv->query($qstr);
	    }
	    /*
	     * Luu chi tieu nhan vien
	     */
	    $ins =1;
	    /*
	     *  lay thong tin de luu cho ke hoach tuan
	     *  khi bo sung chi tieu
	     */
	    $cokhtuan = $this->Chitieunv->Kehoach->find('list',array(
	             									'conditions'=>array('Kehoach.id'=>$kehoach_id),
	             									'fields'=>array('ngaybd','ngaykt','donvi_id')));
	    $dv_id = key($cokhtuan);
	    $ngaybdcha= key($cokhtuan[$dv_id]);
	    $ngayktcha = $cokhtuan[$dv_id][$ngaybdcha];
	    $khcon= $this->Chitieunv->Kehoach->find('list',array(
	    									'conditions'=>array('donvi_id'=>$dv_id,
	    														'ngaybd >='=>$ngaybdcha,
	    														'ngaykt <='=>$ngayktcha,
	    														'DATEDIFF(ngaykt,ngaybd) <='=>7)));
		/*
		 * bat dau luu
		 */
	    $co =0;
	    for($i=0;$i<$colnum2;$i++)
		{
			for($j=0;$j<$rownum;$j++)
			{
				if(isset($this->data['Chitieunv']['col_2']['id'][$j][$i]))
				{
					$record['Chitieunv']['id']=$this->data['Chitieunv']['col_2']['id'][$j][$i];
					$ins=0;
				} 
				$nhanvien_id = $this->data['Chitieunv']['col2'][$i];
				
	        		$hanghoa_id = $this->data['Chitieunv']['row'][$j];
	        		
	             $record['Chitieunv']['kehoach_id']=$kehoach_id;
	             $record['Chitieunv']['nhanvien_id']=$nhanvien_id;
	             $record['Chitieunv']['hanghoa_id']=$hanghoa_id;
	             if ($this->data['Chitieunv']['col_2'][$j][$i]!=NULL)
	                $record['Chitieunv']['soluong']=$this->data['Chitieunv']['col_2'][$j][$i];
	             else
	                $record['Chitieunv']['soluong']=0;
	             if(!$this->Chitieunv->saveAll($record))
	             	$this->Session->setFlash(__('Lưu Thất Bại', true));
	             if ($ins==1)
	             {
	             	/*
	             	 * kiem tra da giao chi tieu tuan cho nv chua
	             	 * neu co them chi tieu vua bo sung vao chi tieu tuan
	             	 */
	             	foreach ($khcon as $keykhcon => $valuekhcon):
	             		$chttuan = $this->Chitieunv->find('list',array(
	             									'conditions'=>array('kehoach_id'=>$keykhcon),
	             									'fields'=>array('hanghoa_id')));
	             														//'nhanvien_id'=>$nhanvien_id,
	             														//'hanghoa_id'=>$hanghoa_id)));
		             	/*
		             	 * luu chi tieu cho tuan
		             	 */
		             	if (!empty($chttuan))
		             	{
		             		if(empty($chttuan[$keykhcon][$hanghoa_id]))
		             		{
		             			$datacttuan['Chitieunv']['kehoach_id']=$keykhcon;
		             			$datacttuan['Chitieunv']['nhanvien_id']=$nhanvien_id;
		             			$datacttuan['Chitieunv']['hanghoa_id']=$hanghoa_id;
		             			$datacttuan['Chitieunv']['soluong']=0;
		             			$co =1;
		             			if(!$this->Chitieunv->saveAll($datacttuan))
		             				$this->Session->setFlash(__('Thêm Vào Chỉ Tiêu Tuần Thất Bại', true));
		             		}
		             	}
	             	endforeach;
	             }
	    	}
		}
		if ($co==1)
    		$this->Session->setFlash(__('Đã Lập Chỉ Tiêu Cho Nhân Viên <br> (LƯU Ý: Cần Bổ Sung Thêm Số Lượng Trong Các Kế Hoạch Tuần Đã Lập)', true));
    	else 
    		$this->Session->setFlash(__('Đã lập chỉ tiêu cho nhân viên', true));
		$this->redirect(array('action' => 'hienchitieuthang',$kehoach_id));
		}
	}
	
	function hienchitieuthang($kehoach_id=null)
	{
		//$rt=0;
		if ($kehoach_id==null)
		{
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		}
		else 
		{
			if ($this->RequestHandler->isAjax())
			{
				//$rt= $this->giaoct($kehoach_id);
				//$this->Chitieunv->Kehoach->id=$kehoach_id;
				//$this->Chitieunv->Kehoach->saveField('dagiao',1);
			}
			/*
		 	* kiem tra chi nhanh giao chi tieu chua
		 	* neu chua thi ko dc lap
			*/
			$kh = $this->Chitieunv->Kehoach->find('list',array(
										'conditions'=>array('Kehoach.id'=>$kehoach_id),
										'fields'=>array('thoigian_id','donvi_id')));
          $dv = $this->Chitieunv->query('select donvi_id from kehoaches where id='.$kehoach_id);
			$cngiao = $this->Chitieudv->find('list',array(
											'recursive'=>0,'contain'=>array('Kehoach'),
											'conditions'=>array('Chitieudv.donvi_id'=>$kh[key($kh)],'Kehoach.thoigian_id'=>key($kh)),
											'fields'=>array('Kehoach.dagiao'),
											'group'=>array('kehoach_id')));
		$kehoach = $this->Chitieunv->Kehoach->find('list',array('conditions'=>array('Kehoach.id'=>$kehoach_id),
														'fields'=>array('donvi_id','dagiao','id')));
		if (empty($kehoach))
		{
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		}
		$donvi_id = key($kehoach[$kehoach_id]);
		$dagiao = $kehoach[$kehoach_id][$donvi_id];
		$thoigian = $this->Chitieunv->Kehoach->find('list',array(
																'recursive'=>0,'contain'=>array('Thoigian'),
																'conditions'=>array('Kehoach.id'=>$kehoach_id),
																'fields'=>array('Thoigian.ngaybd','Thoigian.ngaykt','Thoigian.id')));
		/*
		 * lay data ve diem chuan
		 */
		$col1[]="Điểm Chuẩn"; 
		$diems =$this->Diemchuan->find('list',array(
								'recursive'=>0,'contain'=>array('Hanghoa'),
								'conditions'=>array('kehoach_id'=>$kehoach_id),
								'fields'=>array('Hanghoa.tenhh','diem')));
		/*
		 *  lay data so luong don vi
		 */	
		$chitieunvs = $this->Chitieunv->find('list',array('recursive'=>0,'contain'=>array('Hanghoa','Nhanvien'),
											'conditions'=>array('kehoach_id'=>$kehoach_id),
											'fields'=>array('Hanghoa.tenhh','Chitieunv.soluong','Nhanvien.tennv')));
		$kehoaches = $this->Chitieunv->Kehoach->Chitieudv->Kehoach->find('list',array('conditions'=>array('id'=>$kehoach_id),
													'fields'=>array('dagiao')));
		$hanghoa = $this->Chitieunv->find('list',array('recursive'=>0,'contain'=>array('Hanghoa'),
											'conditions'=>array('kehoach_id'=>$kehoach_id),
											'fields'=>array('Hanghoa.tenhh','Hanghoa.id')));
		/*
		 * lay hanghoa_id va kehoach_id
		 */
		if (count($chitieunvs)!=0)
		{
			$col_ctnv =array_keys($chitieunvs);
			$col_ctnv[]="Tổng";
			$row_ctnv =Null;

			if (count($col_ctnv)!=0)
				$row_ctnv = array_keys($chitieunvs[$col_ctnv[0]]);
				
			$j=0;
			foreach ($row_ctnv as $key => $value)
			{
				$tong=0;
				for ($i=0;$i<count($col_ctnv)-1;$i++)
				{
					$data2[$j][$i]= $chitieunvs[$col_ctnv[$i]][$row_ctnv[$j]];
					$tong +=$data2[$j][$i];		
				}
				$data2[$j][$i]=$tong;
				$data1[$j][0] = $diems[$value];
				$j++;
			}
		}
		$data = $this->laykhtt_tu_cn($kehoach_id,$donvi_id);
		////
			if (!empty($data))
		{
			$col_ctdv =$data['col_ctdv'];
			$row_ctdv = $data['row_ctdv'];
			$datadv= $data['datadv'];
			$datadc=$data['datadc'];
			$col_dc  = $data['col1'];
		}
		$hanghoas = $this->Chitieunv->find('list',array('recursive'=>0,'conditions'=>array('kehoach_id'=>$kehoach_id),
																					'fields'=>array('Hanghoa.id','Hanghoa.tenhh')));
		
		$this->set(compact('col_dc','col_ctdv','row_ctdv','datadv','datadc','hanghoas'));
		
		$this->set(compact('col_ctnv','col1','row_ctnv','data1','data2','thoigian','kehoach_id','dagiao','hanghoa','cngiao','dv'));
		}
	}
	
	function chonchitieu($kehoach_id=null,$themchitieu=1)
	{
		
		if ($kehoach_id==null)
		{
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		}
		else 
		{
			/*
			 * kiem tra chi nhanh giao chi tieu chua
			 * neu chua thi ko dc lap
			 */
			/*$kh = $this->Chitieunv->Kehoach->find('list',array(
										'conditions'=>array('Kehoach.id'=>$kehoach_id),
										'fields'=>array('thoigian_id','donvi_id')));
			$cngiao = $this->Chitieudv->find('list',array(
											'recursive'=>0,'contain'=>array('Kehoach'),
											'conditions'=>array('Chitieudv.donvi_id'=>$kh[key($kh)],'Kehoach.thoigian_id'=>key($kh)),
											'fields'=>array('Kehoach.dagiao'),
											'group'=>array('kehoach_id')));
			
				if ($cngiao[key($cngiao)]==0)
				{
					$this->Session->setFlash(__('Chỉ Tiêu Chưa Thể Lập Do Chưa Chi Nhánh Chưa Chính Thức Giao', true));
					$this->redirect(array('controller'=>'chitieunvs','action'=>'hienchitieuthang',$kehoach_id));
				}*/
			
			/*
			 * kiem tra da lap ke hoach chua
			 */
			$dalapkh = $this->Chitieunv->Kehoach->find('list',array('conditions'=>array('Kehoach.id'=>$kehoach_id)));
			if(count($dalapkh)==0)
			{
				$this->Session->setFlash(__('Chỉ tiêu chưa thể lập cho chưa chọn thời gian giao chỉ tiêu', true));
				$this->redirect(array('controller'=>'kehoaches','action'=>'index',$this->Session->read('cur_dv')));
			}
			
			$kehoaches = $this->Chitieunv->Kehoach->find('list',array('conditions'=>array('id'=>$kehoach_id),
													'fields'=>array('dagiao')));
			$dagiao = $kehoaches[$kehoach_id];
			if ($dagiao == 1)
			{
				$this->Session->setFlash(__('Chỉ tiêu đã giao không được sửa', true));
				$this->redirect(array('action'=>'hienchitieu',$kehoach_id));
			}
			//lay hang hoa
			if ($themchitieu==1)
			{
				/*
				 * Kiem tra kehoach nay da giao chi tieu chua
				 */
				$dalap=$this->Chitieunv->find('list',array(
												'conditions'=>array('Chitieunv.kehoach_id'=>$kehoach_id)));
				if (count($dalap)!=0)
				{
					$this->Session->setFlash(__('Chỉ tiêu tháng này đã được lập', true));
					$this->redirect(array('controller'=>'chitieunvs','action'=>'hienchitieuthang',$kehoach_id));
				}
				$hanghoas = $this->Chitieunv->Hanghoa->find('list',array('fields'=>array('id','tenhh')));
			}
			else
			{
				$hh_dalap = $this->Chitieunv->find('list',array(
															'conditions'=>array('kehoach_id'=>$kehoach_id),
															'fields'=>array('hanghoa_id'),
															'group'=>array('hanghoa_id')));
				$hanghoas = $this->Chitieunv->Hanghoa->find('list',array(
															'conditions'=>array('NOT'=>array('id'=>$hh_dalap))));
			}

			$donvi_id = $this->Session->read('cur_dv');
			//lay thong tin ke hoach tu chi nhanh
			$data = $this->laykhtt_tu_cn($kehoach_id,$donvi_id);
			////
			$hanghoa =array();
			if (!empty($data))
			{
				$col_ctdv =$data['col_ctdv'];
				$row_ctdv = $data['row_ctdv'];
				$datadv= $data['datadv'];
				$datadc=$data['datadc'];
				$col_dc  = $data['col1'];
				$hanghoa = $data['hanghoa'];
			}
			$this->set(compact('col_dc','col_ctdv','row_ctdv','datadv','datadc','hanghoa'));
			//lay nhan vien
			$nhanvien = $this->Chitieunv->Nhanvien->find('list',array('conditions'=>array('donvi_id'=>$donvi_id,'conlam'=>1),'fields'=>array('tennv')));
			$this->set(compact('kehoach_id','themchitieu','nhanvien','hanghoas','thoigian','kh'));
		}
	}

	function chonchitieubs($kehoach_id=null)
	{
		if ($kehoach_id==null)
		{
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		}
		else
		{
			$spdaco = $this->Chitieunv->find('list',array(
											'conditions'=>array('kehoach_id'=>$kehoach_id),
											'fields'=>array('Chitieunv.hanghoa_id'),
											'group'=>array('Chitieunv.hanghoa_id')));
			
			$nvdaco =$this->Chitieunv->find('list',array(
											'conditions'=>array('kehoach_id'=>$kehoach_id),
											'fields'=>array('Chitieunv.nhanvien_id'),
											'group'=>array('Chitieunv.nhanvien_id')));
			
			$hanghoas = $this->Chitieunv->Hanghoa->find('list',array('conditions'=>array('NOT'=>array('id'=>$spdaco))));
			
			$nhanviens = $this->Chitieunv->Nhanvien->find('list',array('conditions'=>array('id'=>$nvdaco),'fields'=>array('tennv')));			
			$this->set(compact('hanghoas','kehoach_id','nhanviens'));
		}
	}
	
	function chonnhanvienbs($kehoach_id=null)
	{
	if ($kehoach_id==null)
		{
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		}
		else
		{
			if(!empty($this->data))
			{
				$counthh = $this->data['Chitieunv']['countcheckhh'];
				$countnv = $this->data['Chitieunv']['countchecknv'];
				/*
				 * nhan vien
				 */
				for ($i=0;$i<$countnv;$i++)
				{
					if ($this->data['Chitieunv']['checkboxnv'.$i]!=0)
						$nhanviens[$this->data['Chitieunv']['checkboxnv'.$i]]=$this->data['Chitieunv'][$this->data['Chitieunv']['checkboxnv'.$i]];
				}
				/*
				 * hang hoa
				 */
				
				for ($i=0;$i<$counthh;$i++)
				{
						$hanghoas[$this->data['Chitieunv']['hh'.$i]]=$this->data['Chitieunv']['hh'.$i];
				}
				
				/*
			     *  lay thong tin de luu cho ke hoach tuan
			     *  khi bo sung chi tieu
			     */
			    $cokhtuan = $this->Chitieunv->Kehoach->find('list',array(
			             									'conditions'=>array('Kehoach.id'=>$kehoach_id),
			             									'fields'=>array('ngaybd','ngaykt','donvi_id')));
			    $dv_id = key($cokhtuan);
			    $ngaybdcha= key($cokhtuan[$dv_id]);
			    $ngayktcha = $cokhtuan[$dv_id][$ngaybdcha];
			    $khcon= $this->Chitieunv->Kehoach->find('list',array(
			    									'conditions'=>array('donvi_id'=>$dv_id,
			    														'ngaybd >='=>$ngaybdcha,
			    														'ngaykt <='=>$ngayktcha,
			    														'DATEDIFF(ngaykt,ngaybd) <='=>7)));
						
				/*
				 * luu vao chitieunvs
				 */
			    $co=0;
				foreach ($nhanviens as $keynv => $value):
					foreach ($hanghoas as $keyhh => $value):
						$record['Chitieunv']['kehoach_id']=$kehoach_id;
						$record['Chitieunv']['hanghoa_id']=$keyhh;
						$record['Chitieunv']['nhanvien_id']=$keynv;
						$record['Chitieunv']['soluong']=0;
						//save
						if(!$this->Chitieunv->saveAll($record))
	             			$this->Session->setFlash(__('Lưu Thất Bại', true));
	             		
	             		/*
	             		 * neu nhan vien ben tuan neu co 
	             		 */
	             		foreach ($khcon as $keykhcon => $valuekhcon):
		             		$chttuan = $this->Chitieunv->find('list',array(
		             									'conditions'=>array('kehoach_id'=>$keykhcon),
		             									'fields'=>array('nhanvien_id')));
		             														//'nhanvien_id'=>$nhanvien_id,
		             														//'hanghoa_id'=>$hanghoa_id)));
			             	/*
			             	 * luu chi tieu cho tuan
			             	 */
			             	if (!empty($chttuan))
			             	{
			             		if(empty($chttuan[$keykhcon][$keynv]))
			             		{
			             			$datacttuan['Chitieunv']['kehoach_id']=$keykhcon;
			             			$datacttuan['Chitieunv']['nhanvien_id']=$keynv;
			             			$datacttuan['Chitieunv']['hanghoa_id']=$keyhh;
			             			$datacttuan['Chitieunv']['soluong']=0;
			             			$co =1;
			             			if(!$this->Chitieunv->saveAll($datacttuan))
			             				$this->Session->setFlash(__('Thêm Vào Chỉ Tiêu Tuần Thất Bại', true));
			             		}
			             	}
	             		endforeach;
					endforeach;
				endforeach;
				if($co==1)
					$this->Session->setFlash(__('Đã Thêm Nhân Viên Vào Chỉ Tiêu Tháng Với Số Lượng Từng Sản Phẩm Là 0 <br> (LƯU Ý: Cần Bổ Sung Thêm Số Lượng Cho Nhân Viên Trong Các Chỉ Tiêu Tuần Đã Lập)', true));
				else
					$this->Session->setFlash(__('Đã Thêm Nhân Viên Vào Chỉ Tiêu Tháng Với Số Lượng Từng Chỉ Tiêu Là 0', true));
				
				$this->redirect(array('controller'=>'chitieunvs','action'=>'hienchitieuthang',$kehoach_id));
			}
			else
			{
				$spdaco = $this->Chitieunv->find('list',array(
												'conditions'=>array('kehoach_id'=>$kehoach_id),
												'fields'=>array('Chitieunv.hanghoa_id'),
												'group'=>array('Chitieunv.hanghoa_id')));
				
				$nvdaco =$this->Chitieunv->find('list',array(
												'conditions'=>array('kehoach_id'=>$kehoach_id),
												'fields'=>array('Chitieunv.nhanvien_id'),
												'group'=>array('Chitieunv.nhanvien_id')));
				
				$hanghoas = $this->Chitieunv->Hanghoa->find('list',array('conditions'=>array('id'=>$spdaco)));
				
				$donvi_id = $this->Session->read('cur_dv');
				$nhanviens = $this->Chitieunv->Nhanvien->find('list',array('conditions'=>array('NOT'=>array('id'=>$nvdaco),'donvi_id'=>$donvi_id),'fields'=>array('tennv')));			
				$this->set(compact('hanghoas','kehoach_id','nhanviens'));
			}
		}
	}
	
	/*
	 *  chi tieu tuan cua Trung Tam
	 */
	function themchitieutuan($kehoach_id=null)
	{
		
		if ($kehoach_id==null)
		{
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		}
		else 
		{
			/*
			 * kiem tra chi tieu thang da giao chua
			 */
			/*$tgkh = $this->Chitieunv->Kehoach->find('list',array('conditions'=>array('Kehoach.id'=>$kehoach_id),
															'fields'=>array('ngaybd','ngaykt','donvi_id')));
			$khthang =$this->Chitieunv->Kehoach->find('list',array(
													'conditions'=>array('donvi_id'=>key($tgkh),
																		'ngaybd <='=>key($tgkh[key($tgkh)]),'ngaykt >='=>$tgkh[key($tgkh)][key($tgkh[key($tgkh)])]),
													'fields'=>array('dagiao')));
			if ($khthang[key($khthang)]==0)
			{
				$this->Session->setFlash(__('Chỉ Tiêu Tháng Chưa Giao Cho Nhân Viên Không Thể Lập Cho Tuần', true));
				$this->redirect(array('controller'=>'chitieunvs','action'=>'hienchitieu',$kehoach_id));
			}*/
			
			/*
			 * kiem tra chi tieu tuan da lap hay chua
			 */
			$dalapct = $this->Chitieunv->find('list',array('recursive'=>0, 'contain'=>array('Kehoach'),'conditions'=>array('kehoach_id'=>$kehoach_id)));
			if (count($dalapct)!=0)
			{
				$this->Session->setFlash(__('Chỉ Tiêu Tuần Này Đã Được Lập', true));
				$this->redirect(array('controller'=>'chitieunvs','action'=>'hienchitieu',$kehoach_id));
			}
			
			
			
			/*
			 * lay tham so
			 */
			$kehoach = $this->Chitieunv->Kehoach->find('list',array('conditions'=>array('Kehoach.id'=>$kehoach_id),
															'fields'=>array('thoigian_id','donvi_id','id')));
			$thoigian_id=key($kehoach[$kehoach_id]);
			$donvi_id = $kehoach[$kehoach_id][$thoigian_id];
			
			$thoigian = $this->Chitieunv->Kehoach->Thoigian->find('list',array('conditions'=>array('id'=>$thoigian_id),
																	'fields'=>array('ngaybd','ngaykt','id')));
			$tuan_id = key($thoigian);
			$ngay_bdtuan = key($thoigian[$tuan_id]);
			$ngay_kttuan=$thoigian[$tuan_id][$ngay_bdtuan];
			$kehoach_thang = $this->Chitieunv->Kehoach->find('list',array(
															'recursive'=>0,'cotain'=>array('Thoigian'),			
															'conditions'=>array('Thoigian.ngaybd <='=>$ngay_bdtuan,'Thoigian.ngaykt >='=>$ngay_kttuan,
															'donvi_id'=>$donvi_id,'Kehoach.id !='=>$kehoach_id),
															'fields'=>array('Thoigian.ngaybd','Thoigian.ngaykt','id')));
			$kehoach_idthang = key($kehoach_thang);
			
			/*
			 * lay thong tin chi tieu
			 *  danh sach hang hoa
			 *  danh sach nhan vien
			 */
			$nhanviens= $this->Chitieunv->find('list',array(
															'recursive'=>0,'contain'=>array('Nhanvien'),
															'conditions'=>array('kehoach_id'=>$kehoach_idthang),
															'fields'=>array('Nhanvien.id','Nhanvien.tennv'),
															'group'=>array('Nhanvien.id','Nhanvien.manv')));
			
			
			
			/*
			 *	kiem tra chi thang cua tuan nay da lap hay chua
			 */

			if (empty($nhanviens))
			{
				$this->Session->setFlash(__('Chưa Thể Lập Chỉ Tiêu Tuần Do Chỉ Tiêu Tháng Chưa Lập ', true));
				$this->redirect(array('controller'=>'chitieunvs','action'=>'hienchitieuthang',$kehoach_idthang));
			}
			
			/*
			 * chi tieu tuan N-1
			 * group theo nhan vien va hang hoa
			 */
			$resulttemp = $this->Chitieunv->Kehoach->find('list',array('conditions'=>array('Kehoach.id'=>$kehoach_idthang),
												'fields'=>array('thoigian_id','donvi_id','id')));
			$thangid=key($resulttemp[$kehoach_idthang]);
			/*
			 * danh sach hang hoa
			 */
			$hanghoas = $this->Chitieunv->find('list',array(
														'recursive'=>0,'contain'=>array('Hanghoa','Kehoach'),
														'conditions'=>array('Kehoach.thoigian_id'=>$thangid,'Kehoach.donvi_id'=>$donvi_id),
														'fields'=>array('Hanghoa.id','Hanghoa.tenhh'),
														'group'=>array('Hanghoa.id','Hanghoa.tenhh')));
			
			$ngay_bdthang= key($kehoach_thang[key($kehoach_thang)]);
			 $ngay_ktthang=$kehoach_thang[$kehoach_idthang][$ngay_bdthang];
			 $kehoachtuans=$this->Chitieunv->Kehoach->find('list',array(
			 												'recursive'=>0,'contain'=>array('Thoigian'),
			 												'conditions'=>array('OR'=>array(array('Thoigian.ngaykt >='=>$ngay_ktthang),array('Thoigian.ngaybd <' =>$ngay_bdthang),array('Thoigian.ngaykt >'=>$ngay_bdtuan))),
			 												'fields'=>array('Kehoach.id')));
	
			 foreach ($kehoachtuans as $kehoachtuan_id):
			 	$kehoachtuans_id[]=$kehoachtuan_id;
			 endforeach;
			 $dagiao = $this->Chitieunv->find('list',array('conditions'=>array('NOT'=>array('Chitieunv.kehoach_id'=>$kehoachtuans_id)),
			 										'fields'=>array('hanghoa_id','tongsl','nhanvien_id'),
			 										'group'=>array('nhanvien_id','hanghoa_id')));
			 $chitieunvs= $this->Chitieunv->find('all',
						array('recursive'=>0,
							'conditions'=>array('Kehoach.thoigian_id'=>$thangid,'Kehoach.donvi_id'=>$donvi_id),
							'fields'=>array('Chitieunv.hanghoa_id','sum(Chitieunv.soluong) as soluong'),
							'group'=>array('Chitieunv.hanghoa_id')));
			
			$hang=0;
			 $nhanviendagiao = $nhanviens;
			 $col =array_keys($nhanviendagiao);
			 $nhanviendagiao[]="Tổng Chỉ Tiêu Tuần N-1";
			 $nhanviendagiao[]="Tổng Chỉ Tiêu Tháng";
			 foreach ($hanghoas as $key_idhh=>$valuehh):
			 	$tong=0;
			 	for ($i=0;$i<count($nhanviendagiao)-2;$i++)
			 	{
			 		if(isset($dagiao[$col[$i]][$key_idhh]))
						$datasl[$hang][$i] = $dagiao[$col[$i]][$key_idhh];
					else
						$datasl[$hang][$i] =0;
					$tong +=$datasl[$hang][$i];
					$datasl[$hang][$i] = number_format($datasl[$hang][$i],0,',','.');
			 	}
			 	$datasl[$hang][$i] = number_format($tong,0,',','.');
			 	foreach ($chitieunvs as $chitieu)
			 	{
			 		if($chitieu['Chitieunv']['hanghoa_id']==$key_idhh)
			 		{
			 			$datasl[$hang][$i+1] = number_format($chitieu[0]['soluong'],0,',','.');
			 			break;
			 		}
			 	}
			 	$hang++;
			 endforeach;
			 $this->set(compact('kehoach_id','nhanviens','hanghoas','ngay_bdtuan','ngay_kttuan','datasl','nhanviendagiao'));
		}
	}
	
	function suachitieu($kehoach_id=null)
	{
		
		if ($kehoach_id==null)
		{
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		}
		else 
		{
		/*
		 * validate
		 * neu chua lap ke hoach roi thi redirect den
		 * neu da lap chi tieu roi thi redirec den hienthichitieu
		 */
			$kehoach = $this->Chitieunv->Kehoach->find('list',array('conditions'=>array('Kehoach.id'=>$kehoach_id),
														'fields'=>array('thoigian_id','donvi_id','id')));
			$thoigian_id=key($kehoach[$kehoach_id]);
			$donvi_id = $kehoach[$kehoach_id][$thoigian_id];
			
			$dalapkh = $this->Chitieunv->Kehoach->find('list',array('conditions'=>array('Kehoach.id'=>$kehoach_id)));
			if(count($dalapkh)==0)
			$this->redirect(array('controller'=>'kehoaches','action'=>'index',$donvi_id));
			
		$dalapct = $this->Chitieunv->find('list',array('recursive'=>0, 'contain'=>array('Kehoach'),'conditions'=>array('kehoach_id'=>$kehoach_id)));
		if (count($dalapct)==0)
			$this->redirect(array('controller'=>'chitieunvs','action'=>'hienchitieu',$kehoach_id));
		/*
		 * kiem tra chi tieu da giao chua neu giao roi khong dc sua
		 */
		$kehoaches = $this->Chitieunv->Kehoach->find('list',array('conditions'=>array('id'=>$kehoach_id),
													'fields'=>array('dagiao')));
		$dagiao = $kehoaches[$kehoach_id];
		if ($dagiao == 1)
		{
			$this->Session->setFlash(__('Chỉ Tiêu Đã Giao Không Được Sửa', true));
			$this->redirect(array('action'=>'hienchitieu',$kehoach_id));
		}
		/*
		 * lay data ve diem chuan
		 */
		$khs= $this->Chitieunv->Kehoach->Chitieudv->find('list',
					array('recursive'=>0, 'contain'=>array('Kehoach'),
						'conditions'=>array('Kehoach.thoigian_id'=>$thoigian_id,'Chitieudv.donvi_id'=>$donvi_id),
						'fields'=>array('kehoach_id'),
						'group'=>array('kehoach_id')));
		$kh_cha=0;
		if (count($khs)!=0)
			$kh_cha = $khs[key($khs)];
		$col1[]="Điểm Chuẩn"; 
		$diems =$this->Diemchuan->find('list',array(
								'recursive'=>0,'contain'=>array('Hanghoa'),
								'conditions'=>array('kehoach_id'=>$kh_cha),
								'fields'=>array('Hanghoa.tenhh','diem')));
		/////Chi tieu trung tam
		$chitieudvs= $this->Chitieunv->Kehoach->Chitieudv->find('list',
					array('recursive'=>0, 'contain'=>array('Hanghoa','Kehoach','Donvi'),'conditions'=>array('Kehoach.thoigian_id'=>$thoigian_id,'Chitieudv.donvi_id'=>$donvi_id),
						'fields'=>array('Hanghoa.tenhh','Chitieudv.soluong','Donvi.madv')));
		$col_ctdv =array_keys($chitieudvs);
		$row_ctdv =Null;
		if (count($col_ctdv)!=0)
		{
			$row_ctdv = array_keys($chitieudvs[$col_ctdv[0]]);
		}
		for ($j=0;$j<count($row_ctdv);$j++)
		{
			for ($i=0;$i<count($col_ctdv);$i++)
			{
				$datadv[$j][$i]= $chitieudvs[$col_ctdv[$i]][$row_ctdv[$j]];		
			}
			$datadc[$j][0]= $diems[$row_ctdv[$j]];
		}
		$thoigian = $this->Chitieunv->Kehoach->Thoigian->find('list',array('conditions'=>array('id'=>$thoigian_id),'fields'=>array('thoigian')));	
		///////Chi tieu nhan vien
		$chitieunvs = $this->Chitieunv->find('list',array(
											'recursive'=>0,'contain'=>array('Hanghoa','Nhanvien'),
											'conditions'=>array('kehoach_id'=>$kehoach_id),
											'fields'=>array('Hanghoa.tenhh','Chitieunv.soluong','Nhanvien.tennv')));
		$chitieunvs_id = $this->Chitieunv->find('list',array(
											'recursive'=>0,'contain'=>array('Hanghoa','Nhanvien'),
											'conditions'=>array('kehoach_id'=>$kehoach_id),
											'fields'=>array('Hanghoa.tenhh','Chitieunv.id','Nhanvien.tennv')));
		$col_ctnv = $this->Chitieunv->find('list',array(
												'recursive'=>0,'contain'=>array('Nhanvien'),
												'conditions'=>array('kehoach_id'=>$kehoach_id),
												'fields'=>array('Chitieunv.nhanvien_id','Nhanvien.tennv')));
		$row_ctnv = $this->Chitieunv->find('list',array(
												'recursive'=>0,'contain'=>array('Hanghoa'),
												'conditions'=>array('kehoach_id'=>$kehoach_id),
												'fields'=>array('Chitieunv.hanghoa_id','Hanghoa.tenhh')));
		
		$cot=0;
		$dong=0;
		foreach ($col_ctnv as $id_nv=>$valuenv)
		{	
			$dong=0;
			foreach ($row_ctnv as $id_hh=>$valuehh)
			{
				$datanvsl[$dong][$cot] = $chitieunvs[$col_ctnv[$id_nv]][$row_ctnv[$id_hh]];
				$datanvid[$dong][$cot] = $chitieunvs_id[$col_ctnv[$id_nv]][$row_ctnv[$id_hh]];
				$dong++;
			}
			$cot++;
		}	
		
		$this->set(compact('kehoach_id','col_ctnv','col1','row_ctnv','datanvsl','datadc','datanvid','col_ctdv','row_ctdv','datadv','thoigian'));
		}
	}
	
	function luuchitieu($kehoach_id=null)
	{
		if ($kehoach_id==null)
		{
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		}
		else
		{ 
		$colnum = $this->data['Chitieunv']['colnum'];
		$rownum = $this->data['Chitieunv']['rownum'];
		for($i=0;$i<$colnum;$i++)
             for($j=0;$j<$rownum;$j++)
             {
					if(isset($this->data['Chitieunv']['id'][$j][$i]))
						$record['Chitieunv']['id']=$this->data['Chitieunv']['id'][$j][$i]; 
             		$record['Chitieunv']['kehoach_id']=$kehoach_id;
                    $record['Chitieunv']['nhanvien_id']=$this->data['Chitieunv']['col'][$i];
                    $record['Chitieunv']['hanghoa_id']=$this->data['Chitieunv']['row'][$j];
                    if ($this->data['Chitieunv'][$j][$i]!=NULL)
                    	$record['Chitieunv']['soluong']=$this->data['Chitieunv'][$j][$i];
                    else
                    	$record['Chitieunv']['soluong']=0;
                    if(!$this->Chitieunv->saveAll($record))
                    {
                         $this->Session->setFlash(__('Lưu Thất Bại', true));                     
             		}
    		}
    		$this->Session->setFlash(__('Đã Lập Kế Hoạch Cho Nhân Viên', true));
       		$this->redirect(array('action' => 'hienchitieu',$kehoach_id));
	}
	}
	
	function hienchitieu($kehoach_id=null)
	{
		$rt =0;
		if ($kehoach_id==null)
		{
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		}
		else 
		{
			if($this->RequestHandler->isAjax())
			{
				$rt=$this->giaoct($kehoach_id);
			}
			$kehoaches = $this->Chitieunv->Kehoach->find('list',array('conditions'=>array('id'=>$kehoach_id),
														'fields'=>array('dagiao')));
			$dagiao = $kehoaches[$kehoach_id];
			/*
			 *	lay thong tin ve ke hoach 
			 */		
			$kehoach = $this->Chitieunv->Kehoach->find('list',array('conditions'=>array('Kehoach.id'=>$kehoach_id),
															'fields'=>array('thoigian_id','donvi_id','id')));
			$thoigian_id=key($kehoach[$kehoach_id]);
			$donvi_id = $kehoach[$kehoach_id][$thoigian_id];
			/*
			 * validate
			 */
			$dalapkh = $this->Chitieunv->Kehoach->find('list',array(
										'conditions'=>array('Kehoach.id'=>$kehoach_id,'Kehoach.donvi_id'=>$donvi_id)));		
			if(count($dalapkh)==0)
				$this->redirect(array('controller'=>'kehoaches','action'=>'index',$donvi_id));
				
			
			/*
			 * lay data ve diem chuan
			 */
			$khs= $this->Chitieunv->Kehoach->Chitieudv->find('list',
						array('recursive'=>0, 'contain'=>array('Kehoach'),
							'conditions'=>array('Kehoach.thoigian_id'=>$thoigian_id,'Chitieudv.donvi_id'=>$donvi_id),
							'fields'=>array('kehoach_id'),
							'group'=>array('kehoach_id')));
			$kh_cha=0;
			if (count($khs)!=0)
				$kh_cha = $khs[key($khs)];
			$col1[]="Điểm Chuẩn"; 
			$diems =$this->Diemchuan->find('list',array(
									'recursive'=>0,'contain'=>array('Hanghoa'),
									'conditions'=>array('kehoach_id'=>$kh_cha),
									'fields'=>array('Hanghoa.tenhh','diem')));
			/*$dalapct = $this->Chitieunv->find('list',array('recursive'=>0, 'contain'=>array('Kehoach'),'conditions'=>array('kehoach_id'=>$kehoach_id,'Kehoach.donvi_id'=>$donvi_id)));
			if (count($dalapct)!=0)
				$this->redirect(array('controller'=>'chitieunvs','action'=>'hienchitieu',$kehoach_id,$thoigian_id,$donvi_id));*/
			///Ke hoach cua Trung tam
			$chitieudvs= $this->Chitieunv->Kehoach->Chitieudv->find('list',
						array('recursive'=>0, 'contain'=>array('Hanghoa','Kehoach','Donvi'),
							'conditions'=>array('Chitieudv.kehoach_id'=>$kh_cha),
							'fields'=>array('Hanghoa.tenhh','Chitieudv.soluong','Donvi.madv')));
			$col_ctdv =array_keys($chitieudvs);
			$row_ctdv =Null;
			if (count($col_ctdv)!=0)
			{
				$row_ctdv = array_keys($chitieudvs[$col_ctdv[0]]);
			}
			for ($j=0;$j<count($row_ctdv);$j++)
			{
				for ($i=0;$i<count($col_ctdv);$i++)
				{
					$datadv[$j][$i]= $chitieudvs[$col_ctdv[$i]][$row_ctdv[$j]];
				}
				$datadc[$j][0]= $diems[$row_ctdv[$j]];
			}
			///Ke hoach cua nhan vien
			$thoigian = $this->Chitieunv->Kehoach->Thoigian->find('list',array('conditions'=>array('id'=>$thoigian_id),'fields'=>array('thoigian')));
			$chitieunvs = $this->Chitieunv->find('list',array('recursive'=>0,'contain'=>array('Hanghoa','Nhanvien'),
												'conditions'=>array('kehoach_id'=>$kehoach_id),
												'fields'=>array('Hanghoa.tenhh','Chitieunv.soluong','Nhanvien.tennv')));
			if (count($chitieunvs)!=0)
			{
				$col_ctnv =array_keys($chitieunvs);
				$col_ctnv[]="Tổng";
				$row_ctnv =Null;
				if (count($col_ctnv)!=0)
				{
					$row_ctnv = array_keys($chitieunvs[$col_ctnv[0]]);
				}
				for ($j=0;$j<count($row_ctnv);$j++)
				{
					$tong=0;
					for ($i=0;$i<count($col_ctnv)-1;$i++)
					{
						$datanv[$j][$i]= $chitieunvs[$col_ctnv[$i]][$row_ctnv[$j]];
						$tong +=$datanv[$j][$i];
						$datanv[$j][$i] = number_format($datanv[$j][$i],0,',','.');
						//$tong = number_format($tong,0,',','.');								
					}
					$datanv[$j][$i]=number_format($tong,0,',','.');
					
				}
				
			}
			
			
	        $so=$this->Chitieunv->query("select datediff(thoigians.ngaykt,thoigians.ngaybd) as songay from thoigians, kehoaches
	                                    where thoigians.id = kehoaches.thoigian_id
	                                    and kehoaches.id=".$kehoach_id);
	        $songay=$so[0] [0] ['songay'];
	        if ($rt==1)
					$this->Session->setFlash(__('Kế Hoạch Tuần Không Thể Giao Do Kế Hoạch Tháng Chưa Giao', true));
					
			$this->set(compact('col_ctdv','col1','row_ctdv','datadc','datadv','col_ctnv', 'row_ctnv','datanv','thoigian','kehoach_id','dagiao','songay'));
		}
	}

	/*
	 * Utility
	 */
	function hienctduocgiao($kehoach_id=null)
	{
		if ($kehoach_id==null)
		{
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		}
		else 
		{
		$thoigian = $this->Chitieudv->Kehoach->find('list',array(
																'recursive'=>0,'contain'=>array('Thoigian'),
																'conditions'=>array('Kehoach.id'=>$kehoach_id),
																'fields'=>array('Thoigian.ngaybd','Thoigian.ngaykt','Thoigian.id')));
		/*
		 * lay data ve diem chuan
		 */
		$col1[]="Điểm Chuẩn"; 
		$diems =$this->Diemchuan->find('list',array(
								'recursive'=>0,'contain'=>array('Hanghoa'),
								'conditions'=>array('kehoach_id'=>$kehoach_id),
								'fields'=>array('Hanghoa.tenhh','diem')));
		/*
		 *  lay data so luong don vi
		 */	
		$chitieudvs = $this->Chitieudv->find('list',array('recursive'=>0,'contain'=>array('Hanghoa','Donvi'),
											'conditions'=>array('kehoach_id'=>$kehoach_id),
											'fields'=>array('Hanghoa.tenhh','Chitieudv.soluong','Donvi.madv')));
		$kehoaches = $this->Chitieudv->Kehoach->find('list',array('conditions'=>array('id'=>$kehoach_id),
													'fields'=>array('dagiao')));
		/*
		 * lay hanghoa_id va kehoach_id
		 */
		/*$ds = $this->Diemchuan->find('list',array(
											'conditions'=>array('kehoach_id'=>$kehoach_id),
											'fields'=>array('kehoach_id','hanghoa_id')));*/
		$dagiao = $kehoaches[$kehoach_id];
		if (count($chitieudvs)!=0)
		{
			$col_ctdv =array_keys($chitieudvs);
			$col_ctdv[]="Tổng";
			$row_ctdv =Null;
			
			if (count($col_ctdv)!=0)
				$row_ctdv = array_keys($chitieudvs[$col_ctdv[0]]);
				
			$j=0;
			foreach ($row_ctdv as $key => $value)
			{
				$tong=0;
				for ($i=0;$i<count($col_ctdv)-1;$i++)
				{
					$data2[$j][$i]= $chitieudvs[$col_ctdv[$i]][$row_ctdv[$j]];
					$tong +=$data2[$j][$i];		
				}
				$data2[$j][$i]=$tong;
				$data1[$j][0] = $diems[$value];
				$j++;
			}
			
		}
		$this->set(compact('col_ctdv','col1','row_ctdv','data1','data2','thoigian','kehoach_id','dagiao'));
		}
	}
	
	function giaoct($kehoach_id=null)
	{
			$this->layout='ajax';
			$dagiao=0;
			$kh = $this->Chitieunv->Kehoach->find('list',array('conditions'=>array('id'=>$kehoach_id),
												'fields'=>array('ngaybd','ngaykt','donvi_id')));
			$dv_id = key($kh);
			$ngaybd= key($kh[$dv_id]);
			$ngaykt = $kh[$dv_id][$ngaybd];
			if((date('d-m-Y',strtotime($ngaykt))- date('d-m-Y',strtotime($ngaybd))) <=7 )
			{
				$khcha= $this->Chitieunv->Kehoach->find('list',array(
				    									'conditions'=>array('donvi_id'=>$dv_id,
				    														'ngaybd <='=>$ngaybd,
				    														'ngaykt >='=>$ngaykt,
				    														'DATEDIFF(ngaykt,ngaybd) >'=>7),
														'fields'=>array('dagiao')));
				if ($khcha[key($khcha)]==1)
				{
					$this->Chitieunv->Kehoach->id=$kehoach_id;
					$this->Chitieunv->Kehoach->saveField('dagiao',1);
					$dagiao=1;
				}
				else
				{
					//return 1;
					$dagiao=2;
					//$this->Session->setFlash(__('Kế Hoạch Tuần Không Thể Giao Do Kế Hoạch Tháng Chưa Giao', true));
				}
			}
			else
			{
				$donvi_cha = $this->Chitieunv->Kehoach->Chitieudv->find('list',array(
												'recursive'=>0,
												'conditions'=>array('Chitieudv.donvi_id'=>$dv_id,
																'Kehoach.ngaybd'=>$ngaybd,
																'Kehoach.ngaykt'=>$ngaykt),
												'fields'=>array('Kehoach.dagiao')));
				if(!empty($donvi_cha))
				{
					if ($donvi_cha[key($donvi_cha)]==1)
					{
						$this->Chitieunv->Kehoach->id=$kehoach_id;
						$this->Chitieunv->Kehoach->saveField('dagiao',1);
						$dagiao=1;
					}
					else
					{
						$dagiao=3;					
					}
				}else
					{
						$dagiao=3;					
					}
			}
			$this->set(compact('dagiao','kehoach_id'));
	}

	function laykhtt()
	{
		$this->layout='ajax';
		$kehoach_id = $this->data['Chitieunv']['kehoach_id'];
		$thoigian_id = $this->data['Chitieunv']['thang'];
		if(!empty($thoigian_id))
		{
			$donvi_id = $this->Session->read('cur_dv');
			//lay thong tin ke hoach tu chi nhanh
			$data = $this->laykhtt_tu_cn($kehoach_id,$donvi_id);
			////
			if (!empty($data))
			{
				$col_ctdv =$data['col_ctdv'];
				$row_ctdv = $data['row_ctdv'];
				$datadv= $data['datadv'];
				$datadc=$data['datadc'];
				$col_dc  = $data['col1'];
			}
		}
		$this->set(compact('col_dc','col_ctdv','row_ctdv','datadv','datadc'));
	}

	function laykhtt_tu_cn($kehoach_id=null,$donvi_id=0)
	{
		$data = array();
		/*
		 *	lay thong tin ve ke hoach 
		 */		
		$kehoach = $this->Chitieunv->Kehoach->find('list',array('conditions'=>array('Kehoach.id'=>$kehoach_id),
														'fields'=>array('thoigian_id','donvi_id','id')));
		$thoigian_id=key($kehoach[$kehoach_id]);
		
		/// lay hang hoa co lap chi tieu
		$hh= $this->Chitieunv->Kehoach->Chitieudv->find('list',
					array('recursive'=>0, 'contain'=>array('Kehoach','Hanghoa'),
						'conditions'=>array('Kehoach.thoigian_id'=>$thoigian_id,'Chitieudv.donvi_id'=>$donvi_id),
						'fields'=>array('hanghoa_id','hanghoa_id'),
						'group'=>array('hanghoa_id')));
		/*
		 * lay data ve diem chuan
		 */
		$khs= $this->Chitieunv->Kehoach->Chitieudv->find('list',
					array('recursive'=>0, 'contain'=>array('Kehoach'),
						'conditions'=>array('Kehoach.thoigian_id'=>$thoigian_id,'Chitieudv.donvi_id'=>$donvi_id),
						'fields'=>array('kehoach_id'),
						'group'=>array('kehoach_id')));
		if(count($khs)!=0)
		{
			$kh_cha = $khs[key($khs)];
			$col1[]="Điểm Chuẩn"; 
			$diems =$this->Diemchuan->find('list',array(
									'recursive'=>0,'contain'=>array('Hanghoa'),
									'conditions'=>array('kehoach_id'=>$kh_cha),
									'fields'=>array('Hanghoa.tenhh','diem')));
			/////Chi tieu trung tam
			$chitieudvs= $this->Chitieunv->Kehoach->Chitieudv->find('list',
						array('recursive'=>0, 'contain'=>array('Hanghoa','Kehoach','Donvi'),
						'conditions'=>array('Chitieudv.kehoach_id'=>$kh_cha),
							'fields'=>array('Hanghoa.tenhh','Chitieudv.soluong','Donvi.madv')));
			$col_ctdv =array_keys($chitieudvs);
			$row_ctdv =Null;
			if (count($col_ctdv)!=0)
			{
				$row_ctdv = array_keys($chitieudvs[$col_ctdv[0]]);
			}
			for ($j=0;$j<count($row_ctdv);$j++)
			{
				for ($i=0;$i<count($col_ctdv);$i++)
				{
					$datadv[$j][$i]= $chitieudvs[$col_ctdv[$i]][$row_ctdv[$j]];		
				}
				$datadc[$j][0]= $diems[$row_ctdv[$j]];
			}
			$data['col1']=$col1;
			$data['row_ctdv']=$row_ctdv;
			$data['col_ctdv']=$col_ctdv;
			$data['datadv']=$datadv;
			$data['datadc']=$datadc;
			$data['hanghoa']=$hh;
		}
		return $data;
	}
	
	function hienctgiaothangnv($kehoach_id=null)
	{
		if ($kehoach_id==null)
		{
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		}
		else 
		{
		$kehoach = $this->Chitieunv->Kehoach->find('list',array('conditions'=>array('Kehoach.id'=>$kehoach_id),
														'fields'=>array('donvi_id','dagiao','id')));
		$donvi_id = key($kehoach[$kehoach_id]);
		$dagiao = $kehoach[$kehoach_id][$donvi_id];
		$thoigian = $this->Chitieunv->Kehoach->find('list',array(
																'recursive'=>0,'contain'=>array('Thoigian'),
																'conditions'=>array('Kehoach.id'=>$kehoach_id),
																'fields'=>array('Thoigian.ngaybd','Thoigian.ngaykt','Thoigian.id')));	
		/*
		 * lay data ve diem chuan
		 */
		$col1[]="Điểm Chuẩn"; 
		$diems =$this->Diemchuan->find('list',array(
								'recursive'=>0,'contain'=>array('Hanghoa'),
								'conditions'=>array('kehoach_id'=>$kehoach_id),
								'fields'=>array('Hanghoa.tenhh','diem')));
		/*
		 *  lay data so luong don vi
		 */	
		$chitieunvs = $this->Chitieunv->find('list',array('recursive'=>0,'contain'=>array('Hanghoa','Nhanvien'),
											'conditions'=>array('kehoach_id'=>$kehoach_id),
											'fields'=>array('Hanghoa.tenhh','Chitieunv.soluong','Nhanvien.tennv')));
		$kehoaches = $this->Chitieunv->Kehoach->Chitieudv->Kehoach->find('list',array('conditions'=>array('id'=>$kehoach_id),
													'fields'=>array('dagiao')));
		/*
		 * lay hanghoa_id va kehoach_id
		 */
		if (count($chitieunvs)!=0)
		{
			$col_ctnv =array_keys($chitieunvs);
			$col_ctnv[]="Tổng";
			$row_ctnv =Null;

			if (count($col_ctnv)!=0)
				$row_ctnv = array_keys($chitieunvs[$col_ctnv[0]]);
				
			$j=0;
			foreach ($row_ctnv as $key => $value)
			{
				$tong=0;
				for ($i=0;$i<count($col_ctnv)-1;$i++)
				{
					$data2[$j][$i]= $chitieunvs[$col_ctnv[$i]][$row_ctnv[$j]];
					$tong +=$data2[$j][$i];		
				}
				$data2[$j][$i]=$tong;
				$data1[$j][0] = $diems[$value];
				$j++;
			}
		}
		$this->set(compact('col_ctnv','col1','row_ctnv','data1','data2','thoigian','kehoach_id','dagiao'));
		}
	}
	
	function hienctgiaotuannv($kehoach_id=null)
	{
	
		if ($kehoach_id==null)
		{
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		}
		else 
		{
		$kehoaches = $this->Chitieunv->Kehoach->find('list',array('conditions'=>array('id'=>$kehoach_id),
													'fields'=>array('dagiao')));
		$dagiao = $kehoaches[$kehoach_id];
		$kehoach = $this->Chitieunv->Kehoach->find('list',array('conditions'=>array('Kehoach.id'=>$kehoach_id),
														'fields'=>array('thoigian_id','donvi_id','id')));
		$thoigian_id=key($kehoach[$kehoach_id]);
		///Ke hoach cua nhan vien
		$thoigian = $this->Chitieunv->Kehoach->Thoigian->find('list',array('conditions'=>array('id'=>$thoigian_id),'fields'=>array('thoigian')));
		$chitieunvs = $this->Chitieunv->find('list',array('recursive'=>0,'contain'=>array('Hanghoa','Nhanvien'),
											'conditions'=>array('kehoach_id'=>$kehoach_id),
											'fields'=>array('Hanghoa.tenhh','Chitieunv.soluong','Nhanvien.tennv')));
		if (count($chitieunvs)!=0)
		{
			$col_ctnv =array_keys($chitieunvs);
			$col_ctnv[]="Tổng";
			$row_ctnv =Null;
			if (count($col_ctnv)!=0)
			{
				$row_ctnv = array_keys($chitieunvs[$col_ctnv[0]]);
			}
			for ($j=0;$j<count($row_ctnv);$j++)
			{
				$tong=0;
				for ($i=0;$i<count($col_ctnv)-1;$i++)
				{
					$datanv[$j][$i]= $chitieunvs[$col_ctnv[$i]][$row_ctnv[$j]];
					$tong +=$datanv[$j][$i];		
				}
				$datanv[$j][$i]=$tong;
			}
			
		}
		$this->set(compact('col_ctnv', 'row_ctnv','datanv','thoigian','kehoach_id','dagiao'));
		}
	}

	/*
	 * chi nhanh theo doi chi tieu trung tam
	 */
	
	function hienchitieutuantt($kehoach_id=null)
	{
	if ($kehoach_id==null)
		{
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		}
		else 
		{
			$kehoaches = $this->Chitieunv->Kehoach->find('list',array('conditions'=>array('id'=>$kehoach_id),
														'fields'=>array('dagiao')));
			$dagiao = $kehoaches[$kehoach_id];
			/*
			 *	lay thong tin ve ke hoach 
			 */		
			$kehoach = $this->Chitieunv->Kehoach->find('list',array('conditions'=>array('Kehoach.id'=>$kehoach_id),
															'fields'=>array('thoigian_id','donvi_id','id')));
			$thoigian_id=key($kehoach[$kehoach_id]);
			$donvi_id = $kehoach[$kehoach_id][$thoigian_id];
			/*
			 * validate
			 */
			$dalapkh = $this->Chitieunv->Kehoach->find('list',array(
										'conditions'=>array('Kehoach.id'=>$kehoach_id,'Kehoach.donvi_id'=>$donvi_id)));		
			if(count($dalapkh)==0)
				$this->redirect(array('controller'=>'kehoaches','action'=>'index',$donvi_id));
				
			
			/*
			 * lay data ve diem chuan
			 */
			$khs= $this->Chitieunv->Kehoach->Chitieudv->find('list',
						array('recursive'=>0, 'contain'=>array('Kehoach'),
							'conditions'=>array('Kehoach.thoigian_id'=>$thoigian_id,'Chitieudv.donvi_id'=>$donvi_id),
							'fields'=>array('kehoach_id'),
							'group'=>array('kehoach_id')));
			$kh_cha=0;
			if (count($khs)!=0)
				$kh_cha = $khs[key($khs)];
			$col1[]="Điểm Chuẩn"; 
			$diems =$this->Diemchuan->find('list',array(
									'recursive'=>0,'contain'=>array('Hanghoa'),
									'conditions'=>array('kehoach_id'=>$kh_cha),
									'fields'=>array('Hanghoa.tenhh','diem')));
			/*$dalapct = $this->Chitieunv->find('list',array('recursive'=>0, 'contain'=>array('Kehoach'),'conditions'=>array('kehoach_id'=>$kehoach_id,'Kehoach.donvi_id'=>$donvi_id)));
			if (count($dalapct)!=0)
				$this->redirect(array('controller'=>'chitieunvs','action'=>'hienchitieu',$kehoach_id,$thoigian_id,$donvi_id));*/
			///Ke hoach cua Trung tam
			$chitieudvs= $this->Chitieunv->Kehoach->Chitieudv->find('list',
						array('recursive'=>0, 'contain'=>array('Hanghoa','Kehoach','Donvi'),
							'conditions'=>array('Chitieudv.kehoach_id'=>$kh_cha),
							'fields'=>array('Hanghoa.tenhh','Chitieudv.soluong','Donvi.madv')));
			$col_ctdv =array_keys($chitieudvs);
			$row_ctdv =Null;
			if (count($col_ctdv)!=0)
			{
				$row_ctdv = array_keys($chitieudvs[$col_ctdv[0]]);
			}
			for ($j=0;$j<count($row_ctdv);$j++)
			{
				for ($i=0;$i<count($col_ctdv);$i++)
				{
					$datadv[$j][$i]= $chitieudvs[$col_ctdv[$i]][$row_ctdv[$j]];
				}
				$datadc[$j][0]= $diems[$row_ctdv[$j]];
			}
			///Ke hoach cua nhan vien
			$thoigian = $this->Chitieunv->Kehoach->Thoigian->find('list',array('conditions'=>array('id'=>$thoigian_id),'fields'=>array('thoigian')));
			$chitieunvs = $this->Chitieunv->find('list',array('recursive'=>0,'contain'=>array('Hanghoa','Nhanvien'),
												'conditions'=>array('kehoach_id'=>$kehoach_id),
												'fields'=>array('Hanghoa.tenhh','Chitieunv.soluong','Nhanvien.tennv')));
			if (count($chitieunvs)!=0)
			{
				$col_ctnv =array_keys($chitieunvs);
				$col_ctnv[]="Tổng";
				$row_ctnv =Null;
				if (count($col_ctnv)!=0)
				{
					$row_ctnv = array_keys($chitieunvs[$col_ctnv[0]]);
				}
				for ($j=0;$j<count($row_ctnv);$j++)
				{
					$tong=0;
					for ($i=0;$i<count($col_ctnv)-1;$i++)
					{
						$datanv[$j][$i]= $chitieunvs[$col_ctnv[$i]][$row_ctnv[$j]];
						$tong +=$datanv[$j][$i];		
					}
					$datanv[$j][$i]=$tong;
				}
				
			}
	        
	        $so=$this->Chitieunv->query("select datediff(thoigians.ngaykt,thoigians.ngaybd) as songay from thoigians, kehoaches
	                                    where thoigians.id = kehoaches.thoigian_id
	                                    and kehoaches.id=".$kehoach_id);
	        $songay=$so[0] [0] ['songay'];
			$this->set(compact('col_ctdv','col1','row_ctdv','datadc','datadv','col_ctnv', 'row_ctnv','datanv','thoigian','kehoach_id','dagiao','songay'));
		}
	}
	
	function hienchitieuthangtt($kehoach_id=null)
	{
	//$rt=0;
		if ($kehoach_id==null)
		{
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		}
		else 
		{
			/*
		 	* kiem tra chi nhanh giao chi tieu chua
		 	* neu chua thi ko dc lap
			*/
			$kh = $this->Chitieunv->Kehoach->find('list',array(
										'conditions'=>array('Kehoach.id'=>$kehoach_id),
										'fields'=>array('thoigian_id','donvi_id')));
          $dv = $this->Chitieunv->query('select donvi_id from kehoaches where id='.$kehoach_id);
			$cngiao = $this->Chitieudv->find('list',array(
											'recursive'=>0,'contain'=>array('Kehoach'),
											'conditions'=>array('Chitieudv.donvi_id'=>$kh[key($kh)],'Kehoach.thoigian_id'=>key($kh)),
											'fields'=>array('Kehoach.dagiao'),
											'group'=>array('kehoach_id')));
		$kehoach = $this->Chitieunv->Kehoach->find('list',array('conditions'=>array('Kehoach.id'=>$kehoach_id),
														'fields'=>array('donvi_id','dagiao','id')));
		$donvi_id = key($kehoach[$kehoach_id]);
		$dagiao = $kehoach[$kehoach_id][$donvi_id];
		$thoigian = $this->Chitieunv->Kehoach->find('list',array(
																'recursive'=>0,'contain'=>array('Thoigian'),
																'conditions'=>array('Kehoach.id'=>$kehoach_id),
																'fields'=>array('Thoigian.ngaybd','Thoigian.ngaykt','Thoigian.id')));
		/*
		 * lay data ve diem chuan
		 */
		$col1[]="Điểm Chuẩn"; 
		$diems =$this->Diemchuan->find('list',array(
								'recursive'=>0,'contain'=>array('Hanghoa'),
								'conditions'=>array('kehoach_id'=>$kehoach_id),
								'fields'=>array('Hanghoa.tenhh','diem')));
		/*
		 *  lay data so luong don vi
		 */	
		$chitieunvs = $this->Chitieunv->find('list',array('recursive'=>0,'contain'=>array('Hanghoa','Nhanvien'),
											'conditions'=>array('kehoach_id'=>$kehoach_id),
											'fields'=>array('Hanghoa.tenhh','Chitieunv.soluong','Nhanvien.tennv')));
		$kehoaches = $this->Chitieunv->Kehoach->Chitieudv->Kehoach->find('list',array('conditions'=>array('id'=>$kehoach_id),
													'fields'=>array('dagiao')));
		$hanghoa = $this->Chitieunv->find('list',array('recursive'=>0,'contain'=>array('Hanghoa'),
											'conditions'=>array('kehoach_id'=>$kehoach_id),
											'fields'=>array('Hanghoa.tenhh','Hanghoa.id')));
		/*
		 * lay hanghoa_id va kehoach_id
		 */
		if (count($chitieunvs)!=0)
		{
			$col_ctnv =array_keys($chitieunvs);
			$col_ctnv[]="Tổng";
			$row_ctnv =Null;

			if (count($col_ctnv)!=0)
				$row_ctnv = array_keys($chitieunvs[$col_ctnv[0]]);
				
			$j=0;
			foreach ($row_ctnv as $key => $value)
			{
				$tong=0;
				for ($i=0;$i<count($col_ctnv)-1;$i++)
				{
					$data2[$j][$i]= $chitieunvs[$col_ctnv[$i]][$row_ctnv[$j]];
					$tong +=$data2[$j][$i];		
				}
				$data2[$j][$i]=$tong;
				$data1[$j][0] = $diems[$value];
				$j++;
			}
		}
		$data = $this->laykhtt_tu_cn($kehoach_id,$donvi_id);
		////
			if (!empty($data))
		{
			$col_ctdv =$data['col_ctdv'];
			$row_ctdv = $data['row_ctdv'];
			$datadv= $data['datadv'];
			$datadc=$data['datadc'];
			$col_dc  = $data['col1'];
		}
		$this->set(compact('col_dc','col_ctdv','row_ctdv','datadv','datadc'));
		
		$this->set(compact('col_ctnv','col1','row_ctnv','data1','data2','thoigian','kehoach_id','dagiao','hanghoa','cngiao','dv'));
		}
	}

	function xoachitieu($kehoach_id=null)
	{
		if(!$kehoach_id)
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		
		$kehoach = $this->Kehoach->find('first',array('recursive'=>-1,'conditions'=>array('Kehoach.id'=>$kehoach_id)));
		$kqbh = $this->Kqbanhang->find('list',array('recursive'=>0,'conditions'=>array('Chitieunv.kehoach_id'=>$kehoach_id)));
		
		$ngaybd =$kehoach['Kehoach']['ngaybd'];
		$ngaykt =$kehoach['Kehoach']['ngaykt'];
		$donvi_id =$kehoach['Kehoach']['donvi_id'];     
		$khcon = $this->Kehoach->find('all',array('recursive'=>-1,'conditions'=>array('donvi_id'=>$donvi_id,
																				'Kehoach.ngaybd >='=>$ngaybd,
																				'Kehoach.ngaykt <='=>$ngaykt,
																				'DATEDIFF(Kehoach.ngaykt,Kehoach.ngaybd) <'=>7)));
		$hanghoas = $this->data['Chitieunv'];
		$duocxoa = 0;
		foreach ($khcon as $khc)
		{
			$khc_id = $khc['Kehoach']['id'];
			$khcdagiao = $khc['Kehoach']['dagiao'];
			$kqbht = $this->Kqbanhang->find('list',array('recursive'=>0,'conditions'=>array('Chitieunv.kehoach_id'=>$khc_id)));
			if(empty($kqbht) && $khcdagiao==0)
			{
				foreach ($hanghoas as $key => $value)
				{
					$this->Chitieunv->deleteAll(array('hanghoa_id'=>$key,'kehoach_id'=>$khc_id));
				}
				$duocxoa = 1;
			}
		}		
		if($kehoach['Kehoach']['dagiao']==0 && empty($kqbh) && $duocxoa==1)
		{
			foreach ($hanghoas as $key => $value)
			{
				if($key == $value)
				{
					$qstr= "delete from diemchuans where hanghoa_id= ".$key." and kehoach_id= ".$kehoach_id;
					$this->Chitieunv->query($qstr);
					$this->Chitieunv->deleteAll(array('hanghoa_id'=>$key,'kehoach_id'=>$kehoach_id));
				}
			}
			$this->Session->setFlash(__('Đã xóa chỉ Tiêu ', true));
		}
		else
		{
			$this->Session->setFlash(__('Chỉ Tiêu Đã Giao hoặc đã báo cáo không Thể Xóa', true));
		}
		
		$this->redirect(array('action'=>'hienchitieuthang',$kehoach_id));
	}
	
}
?>
